'''
Created on May 25, 2010

@author: shemit, egeusz
'''

import math
from java.awt import Color as color

def toradians(degrees):
    return degrees * (math.pi/180.0)

class bone:
    '''
    A class for a rigid object with a fixed length, whose rotation
    and location is modifiable by the rotation of lengthening and 
    shrinking of a muscle
    '''
    def __init__(self, length, attachpttop, attachptbot, toppos, rot):
        '''
        Constructor for a bone
        Length of the bone is the constant length of the bone
        Attach point for the top part of the bone is measured from
        the top to the point of the bone at the highest point 
        where a muscle is attached
        Attach point for the bottom part of the bone is measured from
        toe bottom of the bone to the point of the bone at the lowest
        point where a muscle is attached
        @param length
        @param attachpttop
        @param attachptbot
        @param toppos
        @param rot
        '''
        self.length = length
        self.attachpttop = attachpttop
        self.attachptbot = attachptbot
        self.toppos = toppos
        self.rot = toradians(rot)
        self.newrot = toradians(rot)
        self.botpos = (toppos[0] + length*math.cos(self.rot), 
                       toppos[1] + length*math.sin(self.rot))
        self.vel = (0,0)
        self.rotvel = .05
    def move(self):
        pass
    def gettoppos(self):
        return self.toppos
    def getbotpos(self):
        return self.botpos
    def getbonetop(self):
        return self.bonetop
    def getbonebot(self):
        return self.bonebot
    def computegravity(self, gravity):
        '''
        Currently oversimplifies the pull of gravity by computing
        the rotation if gravity pulls at the center
        '''
        center_of_mass = (self.toppos[0] + (self.length/2)*math.cos(self.rot),
                          self.toppos[1] + (self.length/2)*math.sin(self.rot))
        # TODO: Pendulum problem right here.
        
        # FAKE SOLUTION:
        # Because you know that in a frictionless world all energy is conserved,
        # just rotate back to the same position. The real solution would not 
        # require that you do a check with the original rotation.
        if self.newrot > math.pi - self.rot or self.newrot < self.rot:
            self.rotvel *= -1
        
        self.newrot = self.newrot + self.rotvel
        
        self.botpos = (self.toppos[0] + self.length*math.cos(self.newrot),
                       self.toppos[1] + self.length*math.sin(self.newrot))
        
    def animate(self, graphics, gravity):
        self.computegravity(gravity)
        graphics.drawLine(int(self.toppos[0]), int(self.toppos[1]),
                          int(self.botpos[0]), int(self.botpos[1]))
        
    
class muscle:
    def __init__(self, topbone, botbone, speed):
        self.topbone = topbone
        self.botbone = botbone
        self.speed = speed
    def shrink(self):
        pass
    def extend(self):
        pass
    def animate(self):
        pass
    
class skeleton:
    bones = {}
    def __init__(self):
        self.bones.update({"hanger":bone(150.0, 1.0, 1.0, (150, 0), 45.0)})
    def addbone(self, bone):
        pass
    def attachbones(self, topbone, botbone):
        pass
    def addmuscle(self):
        pass
    def animate(self, graphics, gravity):
        c = graphics.getColor()
        graphics.setColor(color(255, 255, 255))
        self.bones["hanger"].animate(graphics, gravity)
#            graphics.drawLine(int(b.toppos[0]), int(b.toppos[1]), 
#                              int(b.botpos[0]), int(b.botpos[1]))
        graphics.setColor(c)